package com.tedu.webDemo.controller;

import javax.websocket.Decoder.TextStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tedu.webDemo.service.AccessCount;
import com.tedu.webDemo.service.LogInfo;
import com.tedu.webDemo.service.UserCart;
import com.tedu.webDemo.service.UserService;
import com.tedu.webDemo.service.UserServiceImpl;

//控制层
@RestController
@Scope("prototype")
public class UserController {
	
	
	//以前
	//UserController依赖UserServiceImpl
	//UserController被UserServiceImpl控制
	//UserServiceImpl UserServiceImpl=new UserServiceImpl();
	
	//控制层调用业务层,对象是从ioc容器中取
	//对象的类型要写接口，不要写具体的实现类的类型
	//对象从spring框架中来
	//被spring框架控制,依赖框架
	//控制反转 ioc inversion of controller
	@Autowired//默认是根据类型赋值
	//如果框架有2个对象的类型是UserService,赋值失败
	//先根据类型赋值，失败后，再根据对象的名称赋值
	//userServiceImpl,对象
	//userServiceImpl2,对象
	UserService userServiceImpl;
	
	@Autowired
	UserService userServiceImpl2;
	
	@Autowired
	@Qualifier("userServiceImpl2")
	//框架hashmap.get(userServiceImpl2),能取到对象
	//对象赋值给userService
	//先根据类型找到2个对象，赋值失败
	UserService userService;
	@RequestMapping("/test2")
	public String test2() {
		return userServiceImpl.toString()+"<br>"
				+userServiceImpl2.toString()+"<br>"
				+userService.toString();
	}
	
	//从spring框架的ioc容器中取对象
	@Autowired
	AccessCount accessCount;
	
	@Autowired
	LogInfo logInfo;
	
	@Autowired
	UserCart userCart;
	@RequestMapping("/test")
	public String test() {
		return accessCount.toString()+"<br>"
				+logInfo.toString()+"<br>"
				+userCart.toString();
	}
	
	@RequestMapping("/reg")
	//在控制层，业务层方法的第一行加断点
	//main() debug as 
	//console 是否显示 tomcat started on port 8080
	//localhost:8080/register  F6,F8
	public String register(String username) {
		
		int state=userServiceImpl.register(username);
		return "state="+state+this.toString();
	}

}





